<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible" width="800px"
  @close="closeCPGX">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="140px">
    <el-form-item label="关系名称" prop="gzName">
      <el-input ref="focusInput" v-model="dataForm.gzName" placeholder="关系名称" style="width:90%;"></el-input>
    </el-form-item>
    <el-form-item label="关系说明" prop="gzSm">
      <el-input type="textarea" :rows="5" v-model="dataForm.gzSm" placeholder="关系说明" style="width:90%;"></el-input>
    </el-form-item>
    <el-form-item label="被测评对象" prop="gzBpfdx">
      <el-select :disabled="!dataForm.id ? false : true" v-model="dataForm.gzBpfdx">
        <el-option v-for="(item,index) in ['个人','部门']" :key="index" :label="item" :value="item">
        </el-option>
      </el-select>
    </el-form-item>
    <!-- <el-form-item label="评票权重" prop="gzQz">
      <el-input  type="number" v-model="dataForm.gzQz" placeholder="评票权重"></el-input>
    </el-form-item>
    <el-form-item label="评票票数" prop="gzPs">
      <el-input type="number" v-model="dataForm.gzPs" placeholder="评票票数"></el-input>
    </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
      <el-button type="primary" plain @click="closeCPGX">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {getObj, addObj, putObj,  cepingcpgzgzxxbCopyRule} from '@/api/ceping/cepingcpgzgzxxb'
    export default {
    data () {
      return {
        visible: false,
        canSubmit: false,
        dataForm: {
          id: 0,
          gzName: '',
          gzSm: '',
          gzBpfdx: '',
          gzQz: '',
          gzPs: '',
          gzYz: '',
          gzZt: '',
          createTime: '',
          editTime: '',
          createDeptId: '',
          createDeptName: '',
          createUserId: '',
          createUserName: '',
          editDeptId: '',
          editDeptName: '',
          editUserId: '',
          editUserName: '',
          dataState: '',
          tenantId: ''
        },
        dataRule: {
          gzName: [
            { required: true, message: '关系名称不能为空', trigger: 'blur' },
            { min: 1, max: 200, message: '长度在 1 到 200 个字符', trigger: 'blur' }
          ],
          // gzSm: [
          //   { required: true, message: '规则说明不能为空', trigger: 'blur' },

          // ],
          gzBpfdx: [
            { required: true, message: '被测评对象不能为空', trigger: 'blur' }
          ],

          gzPs: [
            { required: true, message: '评票票数不能为空并且只是数字', trigger: 'blur' }
          ]

        },
        isCopy:false,
        addTab:false,
        editTab:false
      }
    },
      methods: {
      init (id,isCopy) {
        if(id&&isCopy==null){
          //编辑
          this.editTab=true;
          this.dataForm.id=id;
        }else if(isCopy){
          this.isCopy=true;
           this.dataForm.id=id;
        }else{
          this.addTab=true;
          this.dataForm.id=0
        }
          //判断是否复用该规则
        this.visible = true;
        this.canSubmit = true;
        this.$nextTick(() => {
          this.$refs.focusInput.focus();
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getObj(this.dataForm.id).then(response => {
                this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.canSubmit = false;
            if (this.editTab) {
                putObj(this.dataForm).then(data => {
                this.$notify.success('修改成功')
                 this.visible = false
                 this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            }else if(this.isCopy){
              //新增规则并复制子项
                 addObj(this.dataForm).then(data => {
                    this.copyRuleHandler(data.data.data,this.dataForm.id);
                   this.visible = false
                   this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            }
             else  {
                addObj(this.dataForm).then(data => {
                    this.$notify.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            }
          this.editTab=false;
          this.isCopy=false;
          this.addTab=false;

          }
        })
      },
    //提交拷贝
    copyRuleHandler(id,oldId) {
        cepingcpgzgzxxbCopyRule(id,oldId).then(data => {
           this.$emit('refreshDataList')
          this.$notify.success('拷贝成功');
        }).catch(() => {
          this.$notify.error("拷贝失败");
        });
    },
      //关闭弹框
      closeCPGX(){
        this.$emit('closeCPGX')
      }
    }
  }
</script>
